package com.yiduo.outpost.ucenter.controller;

import com.yiduo.outpost.core.log.operation.OperationLog;
import com.yiduo.outpost.core.model.entity.User;
import com.yiduo.outpost.core.model.enums.Action;
import com.yiduo.outpost.core.model.result.Result;
import com.yiduo.outpost.ucenter.model.dto.SessionToken;
import com.yiduo.outpost.ucenter.model.dto.SignInRequestBody;
import com.yiduo.outpost.ucenter.model.dto.SignUpRequestBody;
import com.yiduo.outpost.ucenter.service.SessionService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.bind.annotation.AuthenticationPrincipal;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import static com.yiduo.outpost.ucenter.config.Constants.REMEMBER_ME_TTL;

@Slf4j
@RestController
@RequestMapping(path = "/sessions", produces = MediaType.APPLICATION_JSON_VALUE)
@Tag(name = "用户会话")
public class SessionController {

    @Autowired
    private SessionService sessionService;

    /**
     * 登录
     *
     * @return
     * @throws Exception
     */
    @OperationLog(title = "登录", description = "登录系统", action = Action.SIGN_IN)
    @Operation(summary = "登录")
    @PostMapping("/action/sign-in")
    public Result<SessionToken> login(@Validated @RequestBody SignInRequestBody signInRequestBody) {
        log.info("登录:{}", signInRequestBody);
        // throw new RestException(ExceptEnum.RestException);
        Authentication authenticate = sessionService.authenticate(signInRequestBody.getLoginKey(), signInRequestBody.getPassword());
        //登录的时候，要增加一个库的查询
        String token = sessionService.createToken(authenticate, signInRequestBody.getRememberMe() ? REMEMBER_ME_TTL : null);
        return Result.ok(new SessionToken(token));
    }

    /**
     * 注册
     *
     * @param signUpRequestBody
     * @return
     * @throws Exception
     */
    @Operation(summary = "注册")
    @OperationLog(title = "注册", description = "注册账户", action = Action.CREATE)
    @PostMapping("/action/signup")
    public Result<User> signup(@Validated @RequestBody SignUpRequestBody signUpRequestBody) throws Exception {
        User user = sessionService.signUp(signUpRequestBody);
        return Result.ok(user);
    }

    /**
     * 登出
     *
     * @param authorization
     */
    @OperationLog(title = "登出", description = "登出系统", action = Action.LOGOUT)
    @Operation(summary = "登出")
    @DeleteMapping("/action/logout")
    @ResponseStatus(HttpStatus.NO_CONTENT)
    public void logout(@RequestHeader String authorization) {
        //清除redis里的accessToken信息
    }

    @Operation(summary = "通过token获取用户详情")
    @GetMapping("/fetch/by-token")
    public Result<User> fetchByToken(@AuthenticationPrincipal User user) {
        log.info("user:{}", user);
        return Result.ok(user);
    }
}